package com.wngbms.websocket.login;

import lombok.extern.slf4j.Slf4j;
import org.java_websocket.enums.ReadyState;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 单服务节点 登录并发测试
 */
@Slf4j
public class LoginTestSingleNode {

    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        String url = "ws://192.168.31.80:9091/websocket/rxseat01/1234";
        ExecutorService service = Executors.newFixedThreadPool(20);

        for (int i = 0; i < 100; i++) {
            service.submit(() -> {
                try {
                    MyWebSocketClient myClient = new MyWebSocketClient(new URI(url));
                    myClient.connect();
                    // 判断是否连接成功，未成功后面发送消息时会报错
                    while (!myClient.getReadyState().equals(ReadyState.OPEN)) {
                        Thread.sleep(100);
                    }
                } catch (Exception e) {
                    log.error("e", e);
                }
            });
        }

        Thread.sleep(Integer.MAX_VALUE);
    }

}
